package com.gg.fruit.controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

import com.gg.entity.Fruit;
import com.gg.entity.User;
import com.gg.fruit.service.FruitService;
import com.gg.star.service.StarService;

import base.BaseServlet;

@WebServlet("/fruit/GetFruitByFidServlet")
public class GetFruitByFidServlet extends BaseServlet {
	@Autowired
	private  FruitService  fruitService;
	@Autowired
	private  StarService   starService;
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Integer  fid = Integer.parseInt(request.getParameter("fid")) ;
		Fruit  fruit =  new Fruit();
		fruit.setFid(fid);
		
		
		//判断  用户 是否是  登录 状态 
		User  user =  (User) request.getSession().getAttribute("user");
		if(user==null) {
			
			fruit =  fruitService.getFruitByFid(fruit);
		}else {
			fruit =  fruitService.getFruitByFid(fruit);
			//追加   sql  
			String  uid =   user.getUid();
			boolean  res  =  starService.verifyFruitIsStar(fid,uid);
			//把是否关注过  这个 结果     放到 session中
			request.getSession().setAttribute("isStar", res);
		}
		
		
		
		request.getSession().setAttribute("fruit", fruit);
		
		response.sendRedirect("/fruits/fruit_info.jsp");
		
	}
	
}
